package com.acmen.easyapi.service;

import com.acmen.easyapi.executor.ApiDeleteExe;
import com.acmen.easyapi.executor.ApiQueryExe;
import com.acmen.easyapi.model.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author acmen
 */
@Service
public class ApiService {
    @Autowired
    private ApiQueryExe apiQueryExe;
    @Autowired
    private ApiDeleteExe apiDeleteExe;

    public ModelAndView apiDetail(HttpServletRequest request, String apiId, ModelMap model){
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : request.getCookies()) {//遍历cookie数组
                model.put(cookie.getName(), cookie.getValue());
            }
        }
        model.addAllAttributes(apiQueryExe.apiDetail(request,apiId));
       return new ModelAndView("/api/detail",model);
    }
    public Response delApi(String ip, List<String> apiIds){
       return apiDeleteExe.delApi(ip,apiIds);
    }
}
